<template>
	<div class="wrapper">
		<!-- header部分 -->
		<header>
			<p>{{ isBusinessLogin ? '商家登录' : '用户登录' }}</p>
		</header>
		
		<!-- 登录类型切换 -->
		<div class="login-type-switch">
			<div class="switch-tabs">
				<div 
					class="tab-item" 
					:class="{ active: !isBusinessLogin }"
					@click="switchToUserLogin"
				>
					用户登录
				</div>
				<div 
					class="tab-item" 
					:class="{ active: isBusinessLogin }"
					@click="switchToBusinessLogin"
				>
					商家登录
				</div>
			</div>
		</div>
		
		<!-- 表单部分 -->
		<ul class="form-box">
			<li>
				<div class="title">
					{{ isBusinessLogin ? '商家用户名：' : '手机号码：' }}
				</div>
				<div class="content">
					<input 
						type="text" 
						v-model="loginId" 
						:placeholder="isBusinessLogin ? '商家用户名' : '手机号码'"
					>
				</div>
			</li>
			<li>
				<div class="title">
					密码：
				</div>
				<div class="content">
					<input type="password" v-model="password" placeholder="密码">
				</div>
			</li>
		</ul>
		<div class="button-login">
			<button @click="login">登录</button>
		</div>
		<div class="button-register">
			<button @click="register">{{ isBusinessLogin ? '商家注册' : '去注册' }}</button>
		</div>
		<!-- 底部菜单部分 -->
		<Footer></Footer>
	</div>
</template>
<script>
 import Footer from '../components/Footer.vue';
 
 export default{
 name:'Login',
 data(){
 return {
 loginId:'',
 password:'',
 isBusinessLogin: false  // 是否是商家登录模式
 }
 },
 methods:{
 // 切换到用户登录
 switchToUserLogin(){
 this.isBusinessLogin = false;
 this.loginId = '';
 this.password = '';
 },
 
 // 切换到商家登录  
 switchToBusinessLogin(){
 this.isBusinessLogin = true;
 this.loginId = '';
 this.password = '';
 },
 
 login(){
 if(this.loginId==''){
 alert(this.isBusinessLogin ? '商家用户名不能为空！' : '手机号码不能为空！');
 return;
 }
 if(this.password==''){
 alert('密码不能为空！');
 return;
 }
 
 if(this.isBusinessLogin){
 // 商家登录
 this.businessLogin();
 } else {
 // 用户登录
 this.userLogin();
 }
 },
 
 // 用户登录
 userLogin(){
 this.$axios.post('UserController/getUserByIdByPass',this.$qs.stringify({
 userId:this.loginId,
 password:this.password
 })).then(response=>{
 let user = response.data;
 if(user==null){
 alert('用户名或密码不正确！');
 }else{
 //sessionstorage有容量限制，为了防止数据溢出，所以不将userImg数据放入session
 user.userImg = '';
 this.$setSessionStorage('user',user);
 this.$router.go(-1);
 }
 }).catch(error=>{
 console.error(error);
 });
 },
 
 // 商家登录
 businessLogin(){
 this.$axios.post('BusinessController/getBusinessByUsernamePassword',this.$qs.stringify({
 businessUsername:this.loginId,
 businessPassword:this.password
 })).then(response=>{
 let business = response.data;
 if(business==null){
 alert('商家用户名或密码不正确！');
 }else{
 // 保存商家信息到session
 business.businessImg = '';
 this.$setSessionStorage('business',business);
 // 跳转到商家管理页面或者回到上一页
 this.$router.push({path:'/businessManager'});
 }
 }).catch(error=>{
 console.error(error);
 });
 },
 
 register(){
 if(this.isBusinessLogin){
 this.$router.push({path:'businessRegister'});
 } else {
 this.$router.push({path:'register'});
 }
 }
 },
 components:{
 Footer
 }
 }
</script>
<style scoped>
	/****************** 总容器 ******************/
	.wrapper {
		width: 100%;
		height: 100%;
	}

	/****************** header部分 ******************/
	.wrapper header {
		width: 100%;
		height: 12vw;
		background-color: #0097FF;
		color: #fff;
		font-size: 4.8vw;
		position: fixed;
		left: 0;
		top: 0;
		z-index: 1000;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	/****************** 登录类型切换部分 ******************/
	.login-type-switch {
		width: 100%;
		margin-top: 12vw;
		background-color: #f8f8f8;
		border-bottom: 1px solid #ddd;
	}

	.switch-tabs {
		display: flex;
		width: 100%;
	}

	.tab-item {
		flex: 1;
		text-align: center;
		padding: 3vw 0;
		font-size: 3.5vw;
		color: #666;
		background-color: #f8f8f8;
		border-bottom: 2px solid transparent;
		cursor: pointer;
		transition: all 0.3s ease;
	}

	.tab-item.active {
		color: #0097FF;
		background-color: #fff;
		border-bottom-color: #0097FF;
		font-weight: 700;
	}

	.tab-item:hover {
		color: #0097FF;
	}

	/****************** 表单部分 ******************/
	.wrapper .form-box {
		width: 100%;
	}

	.wrapper .form-box li {
		box-sizing: border-box;
		padding: 4vw 3vw 0 3vw;
		display: flex;
		align-items: center;
	}

	.wrapper .form-box li .title {
		flex: 0 0 18vw;
		font-size: 3vw;
		font-weight: 700;
		color: #666;
	}

	.wrapper .form-box li .content {
		flex: 1;
	}

	.wrapper .form-box li .content input {
		border: none;
		outline: none;
		width: 100%;
		height: 4vw;
		font-size: 3vw;
	}

	.wrapper .button-login {
		width: 100%;
		box-sizing: border-box;
		padding: 4vw 3vw 0 3vw;
	}

	.wrapper .button-login button {
		width: 100%;
		height: 10vw;
		font-size: 3.8vw;
		font-weight: 700;
		color: #fff;
		background-color: #38CA73;
		border-radius: 4px;
		border: none;
		outline: none;
	}

	.wrapper .button-register {
		width: 100%;
		box-sizing: border-box;
		padding: 4vw 3vw 0 3vw;
	}

	.wrapper .button-register button {
		width: 100%;
		height: 10vw;
		font-size: 3.8vw;
		font-weight: 700;
		/*与上面登陆按钮不同的只有颜色、背景色、边框不同*/
		color: #666;
		background-color: #EEE;
		border: solid 1px #DDD;
		border-radius: 4px;
		border: none;
		outline: none;
	}
</style>
